//package com.navi.loaddata.loadService.impl.measureData.cell1;
//
//import com.navi.loaddata.core.errorLog.ErrorLog;
//import com.navi.loaddata.core.etlException.ETLException;
//import com.navi.loaddata.core.gpEntity.MppLayoutPnlD;
//import com.navi.loaddata.core.gpEntity.oc.WppCdefectF;
//import com.navi.loaddata.core.gpEntity.oc.WppCdefectGlassF;
//import com.navi.loaddata.core.gpEntity.oc.WppCdefectPanelF;
//import com.navi.loaddata.core.mysqlEntity.EtlConfD;
//import com.navi.loaddata.core.oracleEntity.cell1.EtlCellStepV;
//import com.navi.loaddata.core.oracleEntity.cell1.EtlCellChipV;
//import com.navi.loaddata.core.oracleEntity.cell1.EtlCellDefectV;
//import com.navi.loaddata.core.oracleEntity.cell1.EtlCellComponentV;
//import com.navi.loaddata.core.oracleEntity.cell1.EtlCellProductV;
//import com.navi.loaddata.core.repository.impl.OracleRepositoryImpl;
//import com.navi.loaddata.core.util.LoggerUtil;
//import com.navi.loaddata.core.util.ProcessUtil;
//import com.navi.loaddata.loadService.impl.baseData.cell1.LoadMesBaseDataServiceCELL1;
//import com.navi.loaddata.loadService.impl.baseData.cell1.ProcessBaseDataServiceCELL1;
//import org.hibernate.Query;
//import org.hibernate.SQLQuery;
//import org.hibernate.Session;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Map;
//
//import static com.navi.loaddata.core.constant.GenericDef.SHOP_OC;
//import static java.util.stream.Collectors.groupingBy;
//
///**
// * @author jay
// */
//@Service
//@Transactional(transactionManager = "thirdaryPlatformTransactionManager",
//        rollbackFor = {RuntimeException.class, Exception.class})
//public class LoadMeasureDataServiceCELL1 {
//    private static final Logger logger = LoggerFactory.getLogger(LoadMeasureDataServiceCELL1.class);
//    private LoggerUtil loggerUtil = new LoggerUtil(this.getClass());
//
//    @Autowired
//    private OracleRepositoryImpl dao3;
//
//    @Autowired
//    private LoadMesBaseDataServiceCELL1 loadMesBaseDataService;
//
//    @Autowired
//    private ProcessBaseDataServiceCELL1 processBaseDataService;
//    @Autowired
//    private ProcessDefectDataServiceCELL1 processDefectDataService;
//    @Autowired
//    private LoadDefectInfoServiceCELL1 loadDefectInfoService;
//
//
//    // call by LoadJobMain  @Scheduled(cron = "*/5 * * * * ?")
//    public int loadMeasureDataMain(EtlConfD etlConfD) throws ETLException {
//        int countSize = 0;
//        Session session = dao3.getEntityManagerSession();
//        try {
//            countSize = loadMeasureData(session, etlConfD);
//            logger.info("LoadMeasureDataMain finish");
//        } catch (ETLException e) {
//            throw new ETLException(e.getMessage());
//        }
//        return countSize;
//    }
//
//    /**
//     * Load Measure Data
//     * <p>
//     * load base -> session table
//     * load defect data  -> session table
//     * call base proc  -> auto commit
//     * call defect proc -> no auto commit
//     * delete defect session table
//     *
//     * @param session
//     * @param etlConfD
//     */
//    private int loadMeasureData(Session session, EtlConfD etlConfD) throws ETLException {
//        int countSize = 0;
//
//
//        // defect
//        List<WppCdefectGlassF> defectGlassFEntityList = loadDefectInfoService.loadGlassInfo(etlConfD);
//
//        logger.info("Array defect glass count {}", defectGlassFEntityList.size());
//        if (!defectGlassFEntityList.isEmpty()) {
//            // base
//            List<EtlCellStepV> etlArrayStepVList = new ArrayList<>();
//            List<EtlCellProductV> etlArrayProductVList = new ArrayList<>();
//            try {
//                etlArrayStepVList = loadMesBaseDataService.loadAllFinalStep(SHOP_OC, ErrorLog.ERROR_PROCESS_ARRAY_STEP_V, logger);
//                etlArrayProductVList = loadMesBaseDataService.loadAllFinalProduct(SHOP_OC, ErrorLog.ERROR_PROCESS_ARRAY_PRODUCT_V, logger);
//            } catch (Exception e) {
//                logger.error("Cannot excute loadAll()_BASE from [{}] to [{}]。", etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp());
//                throw new ETLException(e.getMessage());
//            }
//            // save base
//            try {
//                saveAll(etlArrayStepVList, ErrorLog.ERROR_TABLE_ETL_ARRAY_STEP_V, "NULL");
//                saveAll(etlArrayProductVList, ErrorLog.ERROR_TABLE_ETL_ARRAY_PRODUCT_V, "NULL");
//            } catch (Exception e) {
//                logger.error("Cannot excute dao.saveAll()_BASE from [{}] to [{}]。", etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp());
//                throw new ETLException(e.getMessage());
//            }
//
//            // call base proc
//            try {
//                SQLQuery queryLoadStep = session.createSQLQuery("{call CELL1_BASE_LOADER.LOAD_STEP()}");
//                queryLoadStep.executeUpdate();
//            } catch (Exception e) {
//                logger.error(e.getMessage());
//                logger.error(LoggerUtil.stackTraceToString(e));
//                logger.error("Cannot call procedure from [{}] to [{}]。", etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp());
//                throw new ETLException("ALL" + ErrorLog.ERROR_LOAD_ARRAY_STEP_V + e.getMessage());
//            }
//            try {
//                SQLQuery queryLoadProduct = session.createSQLQuery("{call CELL1_BASE_LOADER.LOAD_PRODUCT()}");
//                queryLoadProduct.executeUpdate();
//            } catch (Exception e) {
//                logger.error(e.getMessage());
//                logger.error(LoggerUtil.stackTraceToString(e));
//                logger.error("Cannot call procedure from [{}] to [{}]。", etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp());
//                throw new ETLException("ALL" + ErrorLog.ERROR_LOAD_ARRAY_PRODUCT_V + e.getMessage());
//            }
//
//
//            List<WppCdefectGlassF> curGlassList=   defectGlassFEntityList;
////            Map<String, List<WppCdefectGlassF>> glassDataTree =
////                    defectGlassFEntityList.stream().collect(groupingBy(WppCdefectGlassF::getGlassId));
////            for (Map.Entry<String, List<WppCdefectGlassF>> entry : glassDataTree.entrySet()) {
////                List<WppCdefectGlassF> curGlassList = entry.getValue();
////                //common
////                List<MesProducthistory> commonGlassVList =
////                        loadMesBaseDataService.loadCommonGlassInfo(etlConfD, curGlassList.get(0).getGlassId());
////                if (commonGlassVList.size() == 0) {
////                    continue;
////                }
////                List<EtlCellCmnComponentV> etlArrayCommonGlassVList = new ArrayList<>();
////                try {
////                    etlArrayCommonGlassVList = processBaseDataService.processCommonGlass(commonGlassVList, ErrorLog.ERROR_PROCESS_ARRAY_CMN_GLASS_V, logger);
////                } catch (Exception e) {
////                    logger.error("Cannot excute processCMN() from [{}] to [{}]。", etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp());
////                    throw new ETLException(curGlassList.get(0).getGlassId() + ":" + e.getMessage());
////                }
//////                logger.info("Array common glass count {}", commonGlassVList.size());
////                // save
////                try {
////                    saveAll(etlArrayCommonGlassVList, ErrorLog.ERROR_TABLE_ETL_ARRAY_CMN_GLASS_V, curGlassList.get(0).getGlassId());
////                } catch (Exception e) {
////                    logger.error("Cannot excute dao.saveCMN() from [{}] to [{}]。", etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp());
////                    throw new ETLException(e.getMessage());
////                }
////                // call procedure
////                try {
////                    SQLQuery queryLoadCmnGlass = session.createSQLQuery("{call CELL1_MES_LOADER.LOAD_CMN_GLASS()}");
////                    queryLoadCmnGlass.executeUpdate();
////                } catch (Exception e) {
////                    logger.error(LoggerUtil.stackTraceToString(e));
////                    logger.error("Cannot call procedure from [{}] to [{}]。", etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp());
////                    throw new ETLException(curGlassList.get(0).getGlassId() + ":" + ErrorLog.ERROR_LOAD_ARRAY_CMN_GLASS_V + ":" + e.getMessage());
////                }
//
//            //getMap坐标
//            List<MppLayoutPnlD> layoutPnlDS=new ArrayList<>();
//            try{
//                layoutPnlDS=loadDefectInfoService.big2small(curGlassList.get(0));
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//                for (int i = 0; i < curGlassList.size(); i++) {
//                    //站点限制
//                    if (true == (ProcessUtil.opeLimit(curGlassList.get(i).getOpeId()))) {
//                        continue;
//                    }
//                    Map<String, String> mOpeDList =
//                            loadDefectInfoService.loadMOpeDInfo(etlConfD);
//                    //panel
//                    List<WppCdefectPanelF> defectPanelFEntityList =
//                            loadDefectInfoService.loadPanelInfo(etlConfD, curGlassList.get(i));
//                    logger.info("Array defect panel count {}", defectPanelFEntityList.size());
//                    //defect
//                    List<WppCdefectF> defectFEntityList =
//                            loadDefectInfoService.loadDefectInfo(etlConfD, curGlassList.get(i));
//                    logger.info("Array defect count {}", defectFEntityList.size());
//
//                    List<EtlCellComponentV> etlArrayGlassVList = new ArrayList<>();
//                    List<EtlCellChipV> etlArrayChipVList = new ArrayList<>();
//                    List<EtlCellDefectV> etlArrayDefectVList = new ArrayList<>();
//                    try {
//                        etlArrayGlassVList = processDefectDataService.processGlass(curGlassList.get(i), ErrorLog.ERROR_PROCESS_ARRAY_GLASS_V, logger);
//                        etlArrayChipVList = processDefectDataService.processPanel(defectPanelFEntityList, ErrorLog.ERROR_PROCESS_ARRAY_CHIP_V, logger);
//                        etlArrayDefectVList = processDefectDataService.processDefect(layoutPnlDS,defectFEntityList, mOpeDList, ErrorLog.ERROR_PROCESS_ARRAY_DEFECT_V, logger);
//                    } catch (Exception e) {
//                        logger.error("Cannot excute process() from [{}] to [{}]。", etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp());
//                        throw new ETLException("process错误：" + curGlassList.get(i).getGlassId() + ":" + e.getMessage());
//                    }
//                    // save
//                    try {
//                        saveAll(etlArrayGlassVList, ErrorLog.ERROR_TABLE_ETL_ARRAY_GLASS_V, curGlassList.get(i).getGlassId());
//                        saveAll(etlArrayChipVList, ErrorLog.ERROR_TABLE_ETL_ARRAY_CHIP_V, curGlassList.get(i).getGlassId());
//                        saveAll(etlArrayDefectVList, ErrorLog.ERROR_TABLE_ETL_ARRAY_DEFECT_V, curGlassList.get(i).getGlassId());
//                    } catch (Exception e) {
//                        logger.error("Cannot excute dao.saveAll() from [{}] to [{}]。", etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp());
//                        throw new ETLException(e.getMessage());
//                    }
//
//                    // call proc
//                    try {
//                        SQLQuery queryDefect = session.createSQLQuery("{Call CELL1_FILE_LOADER.LOAD_DATA()}");
//                        queryDefect.executeUpdate();
//                    } catch (Exception e) {
//                        logger.error(ErrorLog.ERROR_LOAD_ARRAY_DATA + ":" + e.getMessage());
//                        logger.error(LoggerUtil.stackTraceToString(e));
//                        logger.error("Cannot call procedure from [{}] to [{}]。", etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp());
//                        throw new ETLException(curGlassList.get(i).getGlassId() + ":" + ErrorLog.ERROR_LOAD_ARRAY_DATA + ":" + e.getMessage());
//                    }
//                    countSize++;
//                    try {
//                        String hqlGlass = "Delete FROM EtlCellComponentV";
//                        String hqlPanel = "Delete FROM EtlCellChipV";
//                        String hqlDefect = "Delete FROM EtlCellDefectV";
//                        Query qGlass = session.createQuery(hqlGlass);
//                        Query qPanel = session.createQuery(hqlPanel);
//                        Query qDefect = session.createQuery(hqlDefect);
//                        qGlass.executeUpdate();
//                        qPanel.executeUpdate();
//                        qDefect.executeUpdate();
////                        qCMN.executeUpdate();
////                        logger.info("删除DEFECT成功！");
//                    } catch (Exception e) {
//                        logger.error(LoggerUtil.stackTraceToString(e));
//                        throw new ETLException("删除DEFECT错误:" + e.getMessage());
//                    }
//                }
////                try {
////                    String hqlCMN = "Delete FROM EtlCellCmnComponentV";
////                    Query qCMN = session.createQuery(hqlCMN);
////                    qCMN.executeUpdate();
//////                    logger.info("删除CMN成功！");
////                } catch (Exception e) {
////                    logger.error(LoggerUtil.stackTraceToString(e));
////                    throw new ETLException("删除CMN错误:" + e.getMessage());
////                }
////            }
//
//        } else {
//            logger.info("没有此区间的数据。"); // todo print log & save etl data
//        }
////        测试
////        List test=dao3.find(EtlCellComponentV.class,"from EtlCellComponentV");
////        System.out.println("===="+test.size());
//        return countSize;
//    }
//
//    public <T> void saveAll(List<T> entities, int errorViewLog, String glassId) throws ETLException {
//        try {
//            dao3.saveAll(entities);
//        } catch (Exception e) {
//            logger.error(errorViewLog + ":" + e.getMessage());
//            logger.error(LoggerUtil.stackTraceToString(e));
//            throw new ETLException(glassId + ":" + errorViewLog + ":" + e.getMessage());
//        }
//    }
//
////    private String getGlassInCondition(List<WppCdefectGlassF> wppAdefectGlassFEntityList) {
////        StringBuffer glassIds = new StringBuffer();
////        int size = wppAdefectGlassFEntityList.size();
////        for (int i = 0; i < size; i++) {
////            glassIds.append("'").append(wppAdefectGlassFEntityList.get(i).getGlassId()).append("'");
////            if (i < size - 1) {
////                glassIds.append(",");
////            }
////        }
////        return glassIds.toString();
////    }
//}
